/*
 * Created by IntelliJ IDEA.
 * User: jbirchfield
 * Date: Mar 20, 2002
 * Time: 9:07:51 AM
 */

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

public class SecretSocket {

    private Key key = null;
    private Cipher outCipher = null;
    private Cipher inCipher = null;
    private CipherInputStream cis = null;
    private CipherOutputStream cos = null;

    private Socket socket = null;

    private String algorithm = "DES";

    public SecretSocket(Socket socket, Key key)  {
        this.socket = socket;
        this.key = key;
        algorithm = key.getAlgorithm();
        initializeCipher();

    }

    private void initializeCipher() {
        try {
            outCipher = Cipher.getInstance(algorithm);
            outCipher.init(Cipher.ENCRYPT_MODE, key);
            inCipher = Cipher.getInstance(algorithm);
            inCipher.init(Cipher.DECRYPT_MODE, key);
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        catch (InvalidKeyException e) {
            e.printStackTrace();
        }

    }

    public InputStream getInputStream() throws IOException {
        InputStream is = socket.getInputStream();
        cis = new CipherInputStream(is, inCipher);
        return cis;
    }

    public OutputStream getOutputStream() throws IOException {
        OutputStream os = socket.getOutputStream();
        cos = new CipherOutputStream(os, outCipher);
        return cos;
    }


}
